package cn.com.kblog.config;

import cn.com.kblog.intercepor.LoginCheckInterceptor;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.ext.plugin.tablebind.AutoTableBindPlugin;
import com.jfinal.ext.route.AutoBindRoutes;
import com.jfinal.render.ViewType;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

/**
 * @author KK.Luo
 * Email luokai8@vip.qq.com
 * @date 2013-11-25下午11:35:38
 */
public class JFWebConfig extends JFinalConfig {

	
	@Override
	public void configConstant(Constants me) {
		loadPropertyFile("app.properties");
		me.setDevMode(getPropertyToBoolean("devMode", true));
		//设置视图类型为FreeMarker
		me.setViewType(ViewType.FREE_MARKER);
		me.setFreeMarkerViewExtension(".html");
		me.setError404View("/WEB-INF/pages/common/404.html");
		me.setError500View("/WEB-INF/pages/common/500.html");
	}

	@Override
	public void configRoute(Routes me) {
		//自动绑定路由
		me.add(new AutoBindRoutes());
	}

	@Override
	public void configPlugin(Plugins me) {
		//检测是否开发模式
		boolean isDev = getPropertyToBoolean("devMode");
		// 配置数据源
        MysqlDataSource ds = new MysqlDataSource();
        if (isDev) {
            ds.setUrl(getProperty("dev.url"));
            ds.setUser(getProperty("dev.username"));
            ds.setPassword(getProperty("dev.password"));
        } else {
        	ds.setUrl(getProperty("bae.url"));
            ds.setUser(getProperty("bae.username"));
            ds.setPassword(getProperty("bae.password"));
        }
		//启动自动注册 继承ActiveRecordPlugin
		AutoTableBindPlugin atbp = new AutoTableBindPlugin(ds);
		atbp.setShowSql(true);
		me.add(atbp);
	}

	@Override
	public void configInterceptor(Interceptors me) {
		//正则表达 开启事务
		//me.add(new TxByRegex(".*[(save)|(delete)|(update)]*.*", true));
		//登陆拦截
		me.add(new LoginCheckInterceptor());
		//me.add(new ContextInterceptor("http://"+AppConfigUtil.get("domain.name")));
	}

	@Override
	public void configHandler(Handlers me) {
		
	}
   
	public static void main(String[] args) {
		JFinal.start("src/main/webapp", 8080, "/", 5);
	}
}
